<?php

namespace api\modules\v1\controllers\work;

use common\models\base\Profession;
use common\models\clinic\Clinic;
use common\models\common\Config;
use common\models\marketing\OutpatientType;
use common\models\patient\Callback;
use services\common\CommonService;
use services\patient\CallbackService;
use Yii;
use api\controllers\OnAuthController;
use yii\db\ActiveRecord;
use yii\web\HttpException;

class TodoController extends OnAuthController
{

    public $modelClass = [];


    /**
     * v1/work/index 待办列表
     * $page
     * $pageSize
     * $outpatient_type_id 就诊类型
     * $keywords 关键词
     * $tags_id 标签
     *
     * @return mixed|ActiveRecord
     */
    public function actionIndexTodo()
    {
        $params = Yii::$app->request->get();
        $member = Yii::$app->user->identity->member;
        $params['belong_member_id'] = (int)$member->id;
        $params['project_id'] = $this->projectId;
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 15;
        $params['re_todo'] = 1;

        $attr = Yii::$app->services->attr->findInfo([
            'member_id' => Yii::$app->user->identity->member_id,
            'project_id' => $this->projectId,
        ]);
        // 是否返回客服就诊类型
        $isCustomer = 0;
        if ($attr && $attr->profession_id == Profession::ID_CUSTOMER) {// 客服
            $isCustomer = 1;
            $params['customer'] = 1;
        } elseif ($attr && $attr->profession_id == Profession::ID_MEDICAL) {// 医助
            $params['customer'] = 2;
        } else {
            $params['customer'] = 3;
        }

        $ids = [];
        if ($isCustomer == 1) {// 客服就诊类型
            $config = Yii::$app->services->config->findInfo([
                'config_name' => Config::CONFIG_NAME_CUSTOMER_OUTPATIENT,
                'project_id' => $this->projectId
            ]);

            if ($config && $config->config_value) {
                $ids = explode(',', $config->config_value);
                $params['outpatient_type_id'] = $params['outpatient_type_id'] ?? $ids;
            }
        }

        $list = Yii::$app->services->patientCallback->getListPage($params, $page, $pageSize, 2);
        $list['list'] = Yii::$app->services->patientCallback->getListPageFormat($list['list'], 0, $isCustomer);

        $callbackData = Yii::$app->services->patientCallback->callbackData($this->projectId, $member->id, $isCustomer);
        $callbackData['belong_member_id'] = (int)$member->id;

        // 获取筛选条件
        $condition = [
            'tags_list' => CommonService::getTagsList(),
            'outpatient_type_list' => CommonService::getOutpatientTypeList($isCustomer),
        ];

        // 岗位类型
        $professionType = 0;
        if ($attr) {
            if ($attr->profession_id == Profession::ID_CUSTOMER) {// 客服
                $professionType = 1;
            } elseif ($attr->profession_id == Profession::ID_MEDICAL) {// 医助
                $professionType = 2;
            }
        }

        return [
            'list' => $list,
            'marketing_stage' => $condition['outpatient_type_list'],
            'tagsState' => $condition['tags_list'],
            'callbackData' => $callbackData,
            'profession_type' => $professionType,// 用户岗位类型【0其他、1客服、2医助】
        ];

    }

    /**
     * 待完善进度列表
     * @return array
     * @author chenyuebin
     */
    public function actionUnimproved()
    {
        $params = Yii::$app->request->get();
        $member = Yii::$app->user->identity->member;
        $params['belong_member_id'] = (int)$member->id;
        $params['project_id'] = $this->projectId;
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 15;
        $params['re_todo'] = 1;
        $params['un_improved'] = 1;

        // 是否返回客服就诊类型
        $isCustomer = Yii::$app->params['is_customer'];
        // 岗位类型
        $professionType = 0;
        if (Yii::$app->params['is_customer'] == 1) {// 客服
            $params['customer'] = 1;
            $professionType = 1;
        } elseif (Yii::$app->params['is_assistant'] == 1) {// 医助
            $params['customer'] = 2;
            $professionType = 2;
        } else {
            $params['customer'] = 3;
        }

        $ids = [];
        if ($isCustomer == 1) {// 客服就诊类型
            $config = Yii::$app->services->config->findInfo([
                'config_name' => Config::CONFIG_NAME_CUSTOMER_OUTPATIENT,
                'project_id' => $this->projectId
            ]);

            if ($config && $config->config_value) {
                $ids = explode(',', $config->config_value);
                $params['outpatient_type_id'] = $params['outpatient_type_id'] ?? $ids;
            }
        }
        // $params['customer'] = $isCustomer;

        $list = Yii::$app->services->patientCallback->getListPage($params, $page, $pageSize, 2, 1);
        $list['list'] = Yii::$app->services->patientCallback->getListPageFormat($list['list'], 0, $isCustomer);

        // 获取筛选条件
        $params = [
            'get_gender' => 1,
            'get_city' => 1,
            'get_body_parts_business' => 1,
            'get_risk_type' => 1,
            'get_effect_type' => 1,
        ];

        $condition = Yii::$app->services->patientUser->getConditionList($params, $this->projectId, 0);
        $condition['outpatient_type_list'] = CommonService::getOutpatientTypeList($isCustomer);
        $condition['tags_list'] = CommonService::getTagsList();
        $condition['patient_diseases_list'] = CommonService::getDiseasesList();


        return [
            'list' => $list,
            'marketing_stage' => $condition['outpatient_type_list'],
            'tagsState' => $condition['tags_list'],
            'gender' => $condition['gender_list'],
            'city' => $condition['city_list'],
            'patient_diseases' => $condition['patient_diseases_list'],
            'body_parts' => $condition['body_parts_list'],
            'effect_type_list' => $condition['effect_type_list'],
            'risk_type_list' => $condition['risk_type_list'],
            'profession_type' => $professionType,// 用户岗位类型【0其他、1客服、2医助】
        ];
    }

    /**
     * 完善患者信息
     * @return array|mixed
     * @throws HttpException
     * @author chenyuebin
     */
    public function actionDoImproved()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;
        if (!isset($params['patient_id']) || $params['patient_id'] <= 0) {
            throw new HttpException(422, '请选择正确的患者');
        }
        return Yii::$app->services->patientUser->improvedEdit($params);
    }

    /**
     *  v1/work/todo/clinic-list 护士待办-搜索用户
     *  $patient_card_id  诊疗号
     *
     * @return array|ActiveRecord|null
     */
    public function actionClinicList()
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = $this->projectId;
        return Yii::$app->services->clinicTreatmentManage->clinicList($params);
    }

    /**
     * v1/work/todo/add-clinic-wait 添加待治疗
     *
     * $patient_card_id 诊疗号
     * $clinic_project_id 治疗项目,多个逗号隔开
     */
    public function actionAddClinicWait()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;
        return Yii::$app->services->clinicTreatmentManage->addClinicWait($params);
    }

    /**
     * v1/work/todo/wait-list  待录入列表
     *
     * $page
     * $pageSize
     * $keyword  string  搜索诊疗号/患者姓名
     * $clinic_project_id  治疗项目
     * @return array
     */
    public function actionWaitList()
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = $this->projectId;
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 8;

        //治疗项目筛选框
        $clinicProject = Yii::$app->services->clinicProject->findList([
            'select' => 'id as value,name as title',
            'status' => Clinic::STATUS_ENABLE,
            'project_id' => $this->projectId
        ]);

        $list = Yii::$app->services->clinicTreatmentManage->waitList($params, $page, $pageSize);

        return [
            'list' => $list,
            'clinic_project' => $clinicProject,
        ];

    }

    /**
     * v1/work/todo/entry-list  已录入列表
     *
     * $page
     * $pageSize
     * $day  查询 录入的时间
     * $keyword  string  搜索诊疗号/患者姓名
     * $clinic_project_id  治疗项目
     * @return array
     */
    public function actionEntryList()
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = $this->projectId;

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 8;

        //治疗项目筛选框
        $clinicProject = Yii::$app->services->clinicProject->findList([
            'select' => 'id as value,name as title',
            'status' => Clinic::STATUS_ENABLE,
            'project_id' => $this->projectId
        ]);

        $list = Yii::$app->services->clinicTreatmentManage->entryList($params, $page, $pageSize);

        return [
            'list' => $list,
            'clinic_project' => $clinicProject,
        ];

    }

    /**
     * v1/work/todo/new-treatment  最新治疗记录
     *
     * $clinic_project_id 治疗项目
     * @return array|ActiveRecord|null
     */
    public function actionNewTreatment()
    {
        $params = Yii::$app->request->GET();
        $params['project_id'] = $this->projectId;
        return Yii::$app->services->clinicTreatmentManage->newTreatment($params);
    }

    /**
     * v1/work/todo/revocation-treatment  撤销治疗
     *
     * $id 已治疗id
     *
     * @return array
     */
    public function actionRevocationTreatment()
    {
        $params = Yii::$app->request->POST();
        $params['project_id'] = $this->projectId;
        return Yii::$app->services->clinicTreatmentManage->revocationTreatment($params);
    }


}
